package com.regentsoft.cloudback.operateAdmin.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.regentsoft.cloudback.base.BaseResp;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.service.PdaMachineCodeService;
import com.regentsoft.cloudback.util.UdpGetClientMacAddr;
import com.regentsoft.cloudback.vo.req.PdaMachineCodeReq;
import com.regentsoft.cloudback.vo.resp.CompanyAddResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Api
@RestController
@RequestMapping("manager/pdaMachineCode/")
public class PdaMachineCodeController {
	@Autowired
	private PdaMachineCodeService pdaMachineCodeService;
	
	@ApiOperation(value = "绑定机器码和激活码", httpMethod = "POST", response = BaseResp.class, notes = "绑定机器码和激活码")
	@RequestMapping(value="bandPdaMachine",method=RequestMethod.POST)
    public BaseResp bandPdaMachine(HttpServletRequest request,@RequestParam String code,@RequestParam String activationCode) throws Exception {
		PdaMachineCodeReq req = new PdaMachineCodeReq();
		req.setActivationCode(activationCode);
		req.setCode(code);
		CompanyAddResp resp  = new CompanyAddResp();
		if(StringUtils.isEmpty(req.getActivationCode())&&StringUtils.isEmpty(req.getCode())) {
			resp.setCode(ResponseCode.PARAMETERERR);
			resp.setMessage("填写数据不完整");
			return resp;
		}
		if(req.getCode().equals("web")) {//web端
			//获取ip地址
			String ip = getRemortIP(request);
			//获取mac地址
			UdpGetClientMacAddr umac=new UdpGetClientMacAddr(ip);  
			code = umac.GetRemoteMacAddr();  
			if(code.equals("web")||StringUtils.isEmpty(code)) {
				resp.setCode(ResponseCode.FAIL);
				resp.setMessage("web获取机器码失败！ip地址："+ip+code);
				return resp;
			}
			req.setCode(code);
			System.out.println(code);
		}
		int i = pdaMachineCodeService.bandPdaMachine(req.getActivationCode(),req.getCode());
		if(i==0) {
			resp.setCode(ResponseCode.SUCCESS);
		}else if(i==1){
			resp.setCode(ResponseCode.NONE_MACHINE_ESITS);
			resp.setMessage("激活码不存在");
		}else if(i==2){
			resp.setCode(ResponseCode.FAIL);
			resp.setMessage("激活失败");
		}else if(i==3){
			resp.setCode(ResponseCode.NONE_MACHINE);
			resp.setMessage("该激活码使用次数已用完，请联系项目实施人员扩增。");
		}else if(i==4){
			resp.setCode(ResponseCode.MACHINE_ESITS);
			resp.setMessage("已经绑定了该企业,请确认激活码是否输入正确。");
		}else {
			resp.setCode(ResponseCode.FAIL);
			resp.setMessage("激活失败");
		}
        return resp;
    }
	
	@ApiOperation(value = "绑定机器码和激活码IOS", httpMethod = "POST", response = BaseResp.class, notes = "绑定机器码和激活码")
	@RequestMapping(value="bandPdaMachineIOS",method=RequestMethod.POST)
    public BaseResp bandPdaMachineIOS(HttpServletRequest request,@RequestBody PdaMachineCodeReq req) throws Exception {
		CompanyAddResp resp  = new CompanyAddResp();
		if(StringUtils.isEmpty(req.getActivationCode())&&StringUtils.isEmpty(req.getCode())) {
			resp.setCode(ResponseCode.PARAMETERERR);
			resp.setMessage("填写数据不完整");
			return resp;
		}
		int i = pdaMachineCodeService.bandPdaMachine(req.getActivationCode(),req.getCode());
		if(i==0) {
			resp.setCode(ResponseCode.SUCCESS);
		}else if(i==1){
			resp.setCode(ResponseCode.NONE_MACHINE_ESITS);
			resp.setMessage("激活码不存在");
		}else if(i==2){
			resp.setCode(ResponseCode.FAIL);
			resp.setMessage("激活失败");
		}else if(i==3){
			resp.setCode(ResponseCode.NONE_MACHINE);
			resp.setMessage("该激活码使用次数已用完，请联系项目实施人员扩增。");
		}else if(i==4){
			resp.setCode(ResponseCode.MACHINE_ESITS);
			resp.setMessage("已经绑定了该企业,请确认激活码是否输入正确。");
		}else {
			resp.setCode(ResponseCode.FAIL);
			resp.setMessage("激活失败");
		}
        return resp;
    }

	
	
	private String getRemortIP(HttpServletRequest request) 

	{

	String clientIP = request.getHeader("x-forwarded-for");



	if(clientIP == null || clientIP.length() == 0 || "unknown".equalsIgnoreCase(clientIP)) 

	{

	clientIP = request.getHeader("Proxy-Client-IP");

	}



	if(clientIP == null || clientIP.length() == 0 || "unknown".equalsIgnoreCase(clientIP)) 

	{

	clientIP = request.getHeader("WL-Proxy-Client-IP");

	}



	if(clientIP == null || clientIP.length() == 0 || "unknown".equalsIgnoreCase(clientIP)) 

	{

	clientIP = request.getRemoteAddr();

	}



	return clientIP;

	} 

	
}
